using FileManager.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.IO;
using System.Linq;
using System.Windows;

namespace FileManager
{
    public partial class App : Application
    {
        // 数据库路径与AppDbContext.cs中保持一致
        public static string DatabasePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "FileManager.db3");

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            
            // 确保数据库目录存在
            Directory.CreateDirectory(Path.GetDirectoryName(DatabasePath));
            
            // 初始化数据库
            using (var dbContext = new AppDbContext())
            {
                try
                {
                    dbContext.Database.EnsureCreated();
                    // 初始化数据库
                    if (!dbContext.Tags.Any())
                    {
                        dbContext.Tags.AddRange(
                            new Tag { Name = "重要" },
                            new Tag { Name = "工作" },
                            new Tag { Name = "个人" }
                        );
                        dbContext.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"初始化数据库失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    throw;
                }
            }
        }
    }
}